package com.xxcxcxcx.xc_moremob.common.world;

import com.xxcxcxcx.xc_moremob.Config;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraftforge.event.world.BiomeLoadingEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ModEntityGeneration {
    //生物实体生成函数
    public static void onEntitySpawn(final BiomeLoadingEvent event) {
//        addEntityToAllOverworldBiomes(event, EntityRegister.ETHAN.get(),
//                40, 2, 4);
        //设置我们的怪物在平原和冰刺地带生成
//        addEntityToSpecificBiomes(event, EntityRegister.SKILL_BASIC.get(),10,1,2, Biomes.PLAINS);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);
//        addEntityToSpecificBiomes(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.ICE_SPIKES);

        //除了萨瓦纳和沙滩地形以外，这种生物都会生成
//        addEntityToAllBiomesExceptThese(event,EntityRegister.SKILL_BASIC.get(),10,1,2,Biomes.SAVANNA , Biomes.BEACH);
        for (Config.Data lsdata : Config.Config){
            ArrayList<Config.ReadData> SumBiomes = Config.whitelistStrtoArraylist(lsdata.Config);
            for (Config.ReadData data:SumBiomes){
                addEntityToSpecificBiomes(event, (EntityType<?>) lsdata.MOB.get(),data.weight,data.minCount,data.maxCount,data.biome);
            }
        }
    }

    //除了哪些地形不生成该生物 参数:地形加载事件、哪一个生物、生成权重、最小生成几个、最大几个、何种地形
    private static void addEntityToAllBiomesExceptThese(BiomeLoadingEvent event, EntityType<?> type, int weight, int minCount, int maxCount, ResourceKey<Biome>... biomes) {
        // Goes through each entry in the biomes and sees if it matches the current biome we are loading
        boolean isBiomeSelected = Arrays.stream(biomes).map(ResourceKey::location)
                .map(Object::toString).anyMatch(s -> s.equals(event.getName().toString()));
        if(!isBiomeSelected) {
            addEntityToAllBiomes(event, type, weight, minCount, maxCount);
        }
    }

    //特定地形生成该生物 ResourceKey<Biome>... 是指可以输入若干个参数，
    @SafeVarargs
    private static void addEntityToSpecificBiomes(BiomeLoadingEvent event, EntityType<?> type,int weight, int minCount, int maxCount, ResourceKey<Biome>... biomes) {
        // Goes through each entry in the biomes and sees if it matches the current biome we are loading
        boolean isBiomeSelected = Arrays.stream(biomes).map(ResourceKey::location).map(Object::toString).anyMatch(s -> s.equals(event.getName().toString()));
        if(isBiomeSelected) {
            addEntityToAllBiomes(event, type, weight, minCount, maxCount);
        }
    }

    //主世界生成该生物
    private static void addEntityToAllOverworldBiomes(BiomeLoadingEvent event, EntityType<?> type,int weight, int minCount, int maxCount) {
        if(!event.getCategory().equals(Biome.BiomeCategory.THEEND) && !event.getCategory().equals(Biome.BiomeCategory.NETHER)) {
            addEntityToAllBiomes(event, type, weight, minCount, maxCount);
        }
    }

    //地狱不生成该生物
    private static void addEntityToAllBiomesNoNether(BiomeLoadingEvent event, EntityType<?> type,int weight, int minCount, int maxCount) {
        if(!event.getCategory().equals(Biome.BiomeCategory.NETHER)) {
            List<MobSpawnSettings.SpawnerData> base = event.getSpawns().getSpawner(type.getCategory());
            base.add(new MobSpawnSettings.SpawnerData(type,weight, minCount, maxCount));
        }
    }

    //末地不生成该生物
    private static void addEntityToAllBiomesNoEnd(BiomeLoadingEvent event, EntityType<?> type,int weight, int minCount, int maxCount) {
        if(!event.getCategory().equals(Biome.BiomeCategory.THEEND)) {
            List<MobSpawnSettings.SpawnerData> base = event.getSpawns().getSpawner(type.getCategory());
            base.add(new MobSpawnSettings.SpawnerData(type,weight, minCount, maxCount));
        }
    }

    //全地形生成该生物
    private static void addEntityToAllBiomes(BiomeLoadingEvent event, EntityType<?> type,int weight, int minCount, int maxCount) {
        List<MobSpawnSettings.SpawnerData> base = event.getSpawns().getSpawner(type.getCategory());
        base.add(new MobSpawnSettings.SpawnerData(type,weight, minCount, maxCount));
    }
}